package com.hdphone.zljutils.impl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import com.hdphone.zljutils.ZljUtils;
import com.hdphone.zljutils.inter.IDeviceUuidFactory;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * @date: 2021/6/2
 * @email： yzandlj@163.com
 * @author： yangzhi
 * @describe:
 */
public class DeviceUuidFactoryImpl implements IDeviceUuidFactory {

    protected static final String PREFS_FILE = "device_id.xml";
    protected static final String PREFS_DEVICE_ID = "device_id";
    protected static UUID uuid;
    private String mAndroidId;

    public DeviceUuidFactoryImpl() {
        if (uuid == null) {
            synchronized (DeviceUuidFactoryImpl.class) {
                if (uuid == null) {
                    final SharedPreferences prefs = ZljUtils.getApp().getSharedPreferences(PREFS_FILE, 0);
                    final String id = prefs.getString(PREFS_DEVICE_ID, null);

                    //android ID常被认为不可信，因为它有时为null。开发文档中说明了：
                    // 这个ID会改变如果进行了出厂设置。并且，如果某个Andorid手机被Root过的话，这个ID也可以被任意改变
                    @SuppressLint("HardwareIds") final String androidId = Settings.Secure.getString(ZljUtils.getApp().getContentResolver(), Settings.Secure.ANDROID_ID);
                    mAndroidId = androidId;
                    if (id != null) {
                        // Use the ids previously computed and stored in the prefs file
                        uuid = UUID.fromString(id);
                    } else {
                        try {
                            if (!TextUtils.equals("9774d56d682e549c", androidId)) {
                                uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
                            } else {
                                uuid = UUID.nameUUIDFromBytes(getPseudoId().getBytes("utf8"));
                            }
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        // Write the value out to the prefs file
                        if (uuid != null) {
                            prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).apply();
                        }
                    }
                    if (uuid != null)
                        ZljUtils.LOG().d("DeviceUuidFactory", "device id = " + uuid.toString());
                }
            }
        }
    }

    @Override
    public UUID getDeviceUuid() {
        return uuid;
    }

    @Override
    public String getAndroidId() {
        return mAndroidId;
    }

    /**
     * Pseudo-Unique ID, 理论上在任何Android手机中都有效
     * 两个手机应用了同样的硬件以及Rom 镜像, 则才有可能 Pseudo-Unique ID 相同
     *
     * @return
     */
    private String getPseudoId() {
        return new StringBuilder()
                .append(35)
                .append("/")
                .append(Build.BOARD.length() % 10)
                .append("/")
                .append(Build.BRAND.length() % 10)
                .append("/")
                .append(Build.CPU_ABI.length() % 10)
                .append("/")
                .append(Build.DEVICE.length() % 10)
                .append("/")
                .append(Build.DISPLAY.length() % 10)
                .append("/")
                .append(Build.HOST.length() % 10)
                .append("/")
                .append(Build.ID.length() % 10)
                .append("/")
                .append(Build.MANUFACTURER.length() % 10)
                .append("/")
                .append(Build.MODEL.length() % 10)
                .append("/")
                .append(Build.PRODUCT.length() % 10)
                .append("/")
                .append(Build.TAGS.length() % 10)
                .append("/")
                .append(Build.TYPE.length() % 10)
                .append("/")
                .append(Build.USER.length() % 10)
                .append("/")
                .toString();
    }
}
